﻿using System;
using System.Collections.Generic;
using System.Text;
using com.tiyma.httpmoudle;
using com.tiyma.common;
using System.Data;
using com.tiyma.data;
using System.Web;
using com.tiyma.model;

namespace com.tiyma.components
{
    public class UserManager
    {
        UserDao m_user_dao = new UserDao();
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="i_script"></param>
        /// <returns></returns>
        public string RegisterUser(string i_script)
        {
            if (string.IsNullOrEmpty(i_script))
                return "参数错误";

            string[] t_arr = i_script.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);

            if (t_arr.Length != 4)
                return "参数错误";

            return RegisterUser(t_arr[0], t_arr[1], t_arr[2], t_arr[3]);

        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="i_script"></param>
        /// <returns></returns>
        public string UpdateUser(string i_script,long i_user_id)
        {
            if (string.IsNullOrEmpty(i_script))
                return "参数错误";

            string[] t_arr = i_script.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);

            if (t_arr.Length != 5)
                return "参数错误";

         

            string t_birth_day = t_arr[4];
            string[] t_birth_day_arr = t_birth_day.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);

            DateTime t_birthday = DateTime.MaxValue;
            if (t_birth_day_arr != null && t_birth_day_arr.Length == 3)
            {
                if (t_birth_day_arr[0] != "-1" && t_birth_day_arr[1] != "-1" && t_birth_day_arr[2] != "-1")
                {
                    t_birthday = new DateTime(Convert.ToInt32(t_birth_day_arr[0]), Convert.ToInt32(t_birth_day_arr[1]), Convert.ToInt32(t_birth_day_arr[2]));
                }
            }

            UserInfo t_user = new UserInfo();
            t_user.UserId = i_user_id;
            t_user.Password =SystemUtil.MSGFilterImport( t_arr[0]);
            t_user.Phone =SystemUtil.MSGFilterImport( t_arr[1]);
            t_user.Email =SystemUtil.MSGFilterImport( t_arr[2]);
            t_user.NickName =SystemUtil.MSGFilterImport( t_arr[3]);
            t_user.BirthDay = t_birthday;

            try
            {
                int t_ret = m_user_dao.UpdateUser(t_user);
                if (t_ret > 0)
                    return "更新成功!";
                return "更新失败";
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新用户信息失败" + ex.ToString());
                return "程序错误,请稍后重试!";
            }
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <returns></returns>
        public UserInfo GetUserInfoByUserId(long i_user_id)
        {
            try
            {
                return m_user_dao.GetUserInfoByUserId(i_user_id);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取用户信息失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 登陆用户
        /// </summary>
        /// <param name="i_script"></param>
        /// <returns></returns>
        public string LoginUser(string i_script)
        {
            try
            {
                if (string.IsNullOrEmpty(i_script))
                    return "登录失败$信息不全";
                string[] arr = i_script.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
                if (arr == null || arr.Length != 2)
                    return "登录失败$信息不全";
                string i_user_name = SystemUtil.MSGFilterImport(arr[0].ToString());
                string i_pwd = SystemUtil.MSGFilterImport(arr[1].ToString());

                UserInfo t_user = m_user_dao.GetUserInfo(i_user_name, i_pwd);

                if (t_user == null)
                    return "登录失败$登录失败,请检查登录信息!";
                CookieManager.SetLoginUserInfo(string.Format("{0}${1}",t_user.UserId,t_user.UserName));
              
                UserToken token = new UserToken(t_user.UserId, t_user.UserName, t_user.NickName);
                UserLoginHelper.SetUserTicket(t_user.UserId.ToString(), token.ToString());
                return string.Format("{0}${1}", "登录成功", t_user.UserName);
            }
            catch (Exception ex)
            {
                LogHelper.Error("登录出现异常" + ex.ToString());
                return "登录失败$程序出现异常,请稍后重试!";
            }
        }

       

         /// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="i_user_name"></param>
        /// <returns></returns>
        public string CheckUserExists(string i_user_name)
        {
            i_user_name = SystemUtil.MSGFilterImport(i_user_name);
            try
            {
                Consts.ResultType t_result = m_user_dao.CheckUserExists(i_user_name);
                if (t_result == Consts.ResultType.Exists)
                    return "用户名已经存在,请重新输入!";

                return "用户名有效!";
            }
            catch (Exception ex)
            {
                LogHelper.Error("检查用户是否存在失败" + ex.ToString());
                return "检查用户是否存在出现异常,稍后重试!";
            }
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="i_user_name"></param>
        /// <param name="i_user_password"></param>
        /// <param name="i_user_email"></param>
        /// <returns></returns>
        public string RegisterUser(string i_user_name, string i_user_password, string i_user_email, string i_verify_code)
        {
            i_user_name = SystemUtil.MSGFilterImport(i_user_name);
            i_user_password = SystemUtil.MSGFilterImport(i_user_password);
            i_user_email = SystemUtil.MSGFilterImport(i_user_email);
           
            string cookieVfCode = CookieManager.GetVifCode();
            //验证验证码
            string[] cookieVfArr = cookieVfCode.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            if (cookieVfArr == null || cookieVfArr.Length != 3)
                return "验证码格式信息有误";

            //验证时间
            string vfCodeDt = cookieVfArr[0].ToString();
            DateTime dt = DateTime.Now;
            if (!DateTime.TryParse(vfCodeDt, out dt))
                dt = DateTime.MinValue;

            TimeSpan timeSpan = DateTime.Now.Subtract(dt);
            if (timeSpan.Seconds > 60)
                return "验证码已过期";

            //验证验证码信息
            if (string.Compare(i_verify_code, cookieVfArr[1].ToString(), true) != 0)
                return "验证码错误";

            if (string.Compare(SystemUtil.MD5Mac(i_verify_code + Consts.VifCode), cookieVfArr[2].ToString(), true) != 0)
                return "验证码信息错误";

            try
            {
                int t_user_id = 0;
                Consts.ResultType t_result = m_user_dao.RegisterUser(i_user_name, i_user_password, i_user_email, out t_user_id);

                if (t_result==Consts.ResultType.Exists)
                    return "用户名已经存在!";

                if (t_result==Consts.ResultType.Successed)
                {
                    CookieManager.SetLoginUserInfo(string.Format("{0}${1}", t_user_id,i_user_name));
              
                    UserToken token = new UserToken(t_user_id,i_user_name, "");
                    UserLoginHelper.SetUserTicket(t_user_id.ToString(), token.ToString());
                    return "注册成功";
                }

                return "注册失败";
            }
            catch (Exception ex)
            {
                LogHelper.Error("注册用户失败" + ex.ToString());
                return "注册出现异常";
            }
        }

       
        
    }
}
